package com.scfire.security

import java.util.Date
import java.util.SortedSet;

import org.grails.databinding.BindingFormat

import com.scfire.project.Project
import com.scfire.project.SubProject
import com.scfire.project.Comment
import com.scfire.project.Donate
import com.scfire.project.ProjectFeedback
import com.scfire.message.SystemMessage
import com.scfire.cert.CreditRank
import com.scfire.userinfo.*;
import com.scfire.fund.FDonate

/**
 * Copyright 2015 Shudao Info Tech Co. Ltd.  All rights reserved.
 * @author lhzeng
 * @email zenglh@haoshudao.com
 * @date 2015年6月7日 下午1:25:20
 * @version V1.0
 */
class Member {

	transient springSecurityService

	//注册用户名,目前存储的是用户微信的openid
	String username
	//注册密码
	String password
	//用户状态
	boolean enabled = true
	//账户是否过期
	boolean accountExpired
	//账户是否被锁
	boolean accountLocked
	//账户密码是否过期
	boolean passwordExpired
	
	//昵称
	String nickname	
	//性别
	String sex
	//个人头像
	String imagePath	
	//生日
	@BindingFormat('yyyy-MM-dd')
	Date   birthday
	
	//登陆状态
	boolean enrollStatus = false
	
	//经度
	double latitude = 0.0
	//纬度
	double longitude = 0.0
	
	//注册时间
	@BindingFormat('yyyy-MM-dd HH:mm:ss')
	Date   createTime
	//更新时间
	@BindingFormat('yyyy-MM-dd HH:mm:ss')
	Date   updateTime
	
	/**微信登陆接口获取的信息**/
	//微信mobile端OPENID
	String openid
	//微信PC端OPENID
	String pcOpenid
	
	//所在国家
	String country
	//所在省
	String province
	//所在城市
	String city
	//详细地址
	String address
	//微信头像链接地址
	String headimgurl
	//微信UNIONID
	String unionid
	
	/**用户认证信息**/
	//用户真实姓名
	String realName	
	//用户身份证号
	String idNumber
	//手机号
	String mobile
	//身份证正面路径
	String activeIDNumberPhoto
	//身份证背面路径
	String negativeIDNumberPhoto
	//银行卡开户行
	String bankName
	//绑定的银行卡
	String bankCard
	//开户支行
	String subBranch
	//学校信息
	String school
	//个人简介
	String description
	//用户认证标识
	String verify
	
	//预留字段1
	String reserve1
	//预留字段2
	String reserve2
	//预留字段3
	String reserve3
	
	//项目集
	SortedSet projects
	//评论信息
	SortedSet comments
	//捐赠信息
	SortedSet donates
	
	//项目反馈信息集
	SortedSet projectFeedbacks
	
	//系统消息
	SortedSet systemMessages
	
	String otherUnionid
	
	boolean pushFlag= false
	
	//用户微信信息
	static hasOne = [creditRank:CreditRank, addressinfo:Address]
	
	//用户创建的项目集、用户评论信息、用户捐赠信息、用户反馈信息、系统推送信息、用户创建的一起捐信息、用户配捐信息
	//static hasMany = [projects:Project,comments:Comment,donates:Donate,projectFeedbacks:ProjectFeedback,systemMessages:SystemMessage,subProjects:SubProject,fdonates:FDonate]
	
	static transients = ['springSecurityService']

	static constraints = {
		username blank: false, unique: true
		password blank: false
				
		nickname nullable:true
		mobile nullable:true //一个手机号只能注册一个
		subBranch nullable:true
		
		sex nullable:true
		imagePath nullable:true
		birthday nullable:true
		description nullable:true
		enrollStatus nullable:true
		latitude nullable:true
		longitude nullable:true
		createTime nullable:true
		updateTime nullable:true
		
		openid nullable:true
		pcOpenid nullable:true		
		
		country nullable:true
		province nullable:true
		city nullable:true
		address nullable:true
		headimgurl nullable:true
		unionid nullable:true , unique : true
		
		realName nullable:true
		school nullable:true
		idNumber nullable:true
		activeIDNumberPhoto nullable:true
		negativeIDNumberPhoto nullable:true
		bankName nullable:true
		bankCard nullable:true
		subBranch nullable:true
		verify nullable:true
		
		reserve1 nullable:true
		reserve2 nullable:true
		reserve3 nullable:true
		
		projects nullable:true
		comments nullable:true
		donates nullable:true		
		projectFeedbacks nullable:true
		systemMessages nullable:true
		creditRank nullable:true
		addressinfo    nullable:true
		
		//subProjects nullable:true
		//fdonates nullable:true
		pushFlag nullable:true
		otherUnionid nullable:true
	}
	static mapping = {
		password column: '`password`'
	}

	Set<Authority> getAuthorities() {
		MemberAuthority.findAllByMember(this).collect { it.authority }
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
	}
}
